GBML manual / Drawing

Drawing and the draw order

An instance draws its own sprite automatically until you write a Draw event, and after that you must call draw_self() yourself or it disappears.

Read this first: a Draw event switches automatic drawing off

If you add a Draw event to an object and do not call draw_self() in it, that object's sprite stops appearing on screen. Nothing is broken. The game is doing what a Draw event means: you have taken over the drawing of that object.

Drawing calls only work in the drawing events. Every draw_ call (draw_self, draw_sprite, draw_text, draw_rect_fill, draw_background and the rest) is allowed in a Draw, Draw Begin, Draw End or Draw GUI event, and the checker refuses one anywhere else (a Step event, a Create event) and tells you to move it. A script may draw: it takes the event of whoever calls it.

The object hasWhat you see
no Draw eventits sprite, drawn for you every frame
a Draw event with draw_self(); in itits sprite, plus whatever else you draw in the event
a Draw event without draw_self();nothing from its own sprite. Only what your code draws

The fix is one line, put first in the event:

// Draw event of an enemy that has noticed the player
draw_self();
draw_text(x, y - 20, "!");

draw_self() draws the instance exactly the way automatic drawing would. Everything after it is extra: a label, a glow, a held item.

Some details, because this is the most common way to think the tool has stopped working:

What drawing is

The GBA draws two kinds of thing: sprites and backgrounds. This page is about sprites - the pictures that belong to instances. A sprite is a small image, and one instance normally shows one. Backgrounds covers the other kind. The screen is 240 pixels wide and 160 tall. The hardware draws at most 128 sprites in one frame, so every sprite you show spends from a fixed budget. Budgets lists it.

Automatic drawing

Without a Draw event, the engine draws each instance from its own variables:

VariableWhat it does
spritewhich sprite to show. Leave it unset to draw nothing
framewhich picture of a multi-picture sprite (an animation frame) to show
frame_counthow many pictures the sprite has. Read-only
frame_speedhow many animation frames frame moves forward every game frame. 0 (the default) means no animation, 1 changes the picture 60 times a second, 0.5 changes it every other game frame, 0.125 every eighth. Wraps back to the first picture by itself
anglerotation in degrees
xscale, yscalestretch. 1 is normal size, and a negative value mirrors
flip_x, flip_ymirror left-right or top-bottom
visiblefalse hides it
depth, bg_prioritywhat draws in front of what. See below

Rotating or stretching a sprite uses one of the hardware's 32 affine matrices, the small calculators that do the turning, for as long as the sprite is on screen. A plain mirror does not use one. Leave angle at 0 and the scales at 1 on anything that never needs them. Automatic drawing also skips an instance the camera cannot currently see.

Where the sprite appears is the instance's x and y (counted from the room's top-left corner, see Where (0, 0) is), at the sprite's origin. The origin is the spot on the picture that sits exactly at the object's position. It is the center unless the sprite was given a different origin in the Sprites tab.

An object can also choose its own origin: in the object editor, press Set Origin next to the collision mask, then click the picture, type an X and Y, or press Origin Center. It counts in pixels from the top-left corner of one frame and is the same on every frame. The object's own choice wins over the sprite's, but only for that object's own drawing (default drawing and draw_self); draw_sprite draws a sprite you name, so it still uses that sprite's origin. The collision box stays on the picture when you move the origin, so the two never drift apart.

A rectangle is different, because it has no picture and so no origin to pick. draw_rect_fill, draw_rect, and their _anchored forms put the rectangle's top-left corner at the (x, y) you give. So draw_rect_fill(0, 0, screen_width(), screen_height(), COLOR_BLUE) fills the whole screen.

"I moved the origin and nothing happened"

This one catches people, so it is worth knowing before it wastes your afternoon.

Moving the origin changes which spot on the picture sits at the object's position. It does not, on its own, decide where the picture ends up on screen - because something else may be deciding the position.

Say a player falls until it lands on the ground. The collision box stays on the picture (above), so the player stops falling when its art touches the ground, whatever the origin is. Move the origin down by 6 pixels and the player simply rests at a position 6 pixels lower - and lands in exactly the same place on screen. Nothing looks different, and nothing is wrong.

You can see the origin working in the frames before it lands. It really is drawn 6 pixels higher while falling; it is only once the ground decides its position that the difference disappears.

So to see what an origin does, try it on something whose position you control: an object that just sits there, a sprite you place by hand, or your falling player before it touches the ground. Then the picture moves exactly as much as you moved the origin.

And that is the useful way to think about it. The origin is about the point you hold the object by - which matters enormously for rotation, for lining a character up with a tile, and for saying "put its feet here" instead of "put its middle here". It is not a way to nudge a picture around.

Drawing by hand: the Draw events

Once you write a Draw event, you make the drawing calls. Three events draw in world space, meaning they scroll with the camera, and three more draw fixed to the screen. The drawing calls are the same in both: it is the event that decides where a call draws.

EventSpaceRuns
Draw Beginworldfirst
Drawworldthen this. The one that turns automatic drawing off
Draw Endworldthen this
Draw GUI Begin, Draw GUI, Draw GUI Endscreenafter the world events, in that order

The GUI events are for anything that stays put while the room scrolls: a health bar, a score. They draw on a layer of their own: (0, 0) is the top-left pixel of the screen there, and everything drawn in them sits on top of the whole game. See Draw GUI. An event running earlier is not the same as drawing behind. What sits in front is decided by the order rules below.

The drawing calls:

CallDraws
draw_self()this instance, the automatic way
draw_sprite(spr, x, y, frame)any sprite, anywhere, at this instance's own depth
draw_sprite_scaled(...)the same with angle, scale, flips and its own depth
draw_rect_filla filled rectangle, in one of eight COLOR_ constants
draw_recta one-pixel outline
draw_text and its kintext. See Text
show_collision(on), show_collision_all(on)a red outline of the collision box, for debugging

The GBA has no canvas to paint on. Each call above makes a real sprite. draw_rect_fill is one sprite, stretched to the size you asked for. An outline is up to four sprites, one per side. The engine remembers each call from one frame to the next. A call that draws the same thing every frame reuses last frame's sprite, and a call you stop making has its sprite released at the end of that frame. A Draw event that changes every frame costs a real sprite for the change.

Five functions ask about a sprite asset without drawing anything: sprite_width, sprite_height and sprite_frames, plus sprite_origin_x and sprite_origin_y for where its origin is.

Draw order: what covers what

Two settings decide this, and the first beats the second.

  1. bg_priority picks one of four hardware layers, from 0 (nearest the viewer) to 3 (furthest back). The default is 3. A sprite draws in front of a background on its own layer or a layer behind it. It draws behind a background on a layer in front. A tie goes to the sprite.
  2. depth orders sprites against each other on one layer. A higher depth draws further back. The classic top-down use is depth = -y;, so whatever is lower on the screen draws in front.

A third rule applies inside one Draw event that draws a background with draw_background: sprites and backgrounds stack in the order you make the calls, and what you draw later is in front. Draw a sprite and then a big solid background and the sprite is hidden. draw_background says how many switches between sprites and backgrounds fit.

Two things catch people out. depth is grouped into a small number of bands, because the hardware has a limited number of sort layers. Two instances with nearly the same depth land in the same band and draw in the order they were created. And everything drawn in a Draw GUI event is on the front-most layer, so nothing in the world can cover it. Where the machine shows through explains the bands.

See also

Motion and pathing moves the instance that is being drawn. Text, Backgrounds, The camera and Effects cover the rest of what the screen shows.

See also: drawdraw_selfdepthbg_priorityThe order of a frameBudgets